home *** CD-ROM | disk | FTP | other *** search
- Path: info.uah.edu!oreo!gbacon
- From: gbacon@oreo (Greg Bacon)
- Newsgroups: comp.lang.c
- Subject: Re: hex to dec function?
- Date: 6 Mar 1996 23:56:04 GMT
- Organization: The University of Alabama in Huntsville
- Message-ID: <4hl8mk$1er@info.uah.edu>
- References: <4gdh1b$bg@mailhost.mwmicro.com>
- Reply-To: gbacon@CS.UAH.Edu
- NNTP-Posting-Host: oreo.aspire.cs.uah.edu
- X-Newsreader: TIN [version 1.2 PL2]
-
- aschlies@citynet.net wrote:
- : Hi There,
-
- : Is there a function that converts HEX to Dec in ANSI C?
-
- : Why re-invent the wheel?
-
- : Thanks
- : Tony
-
- Recite this mantra with me:
- "All numbers in a digital computer are stored in binary, regardless of
- program representation."
-
- Assuming you have a string representing some hexadecimal quantity and
- you want to print its equivalent decimal value:
-
- #include <stdio.h>
- #include <stdlib.h>
-
- /*
- :
- :
- */
- {
- char *strHexVal = "20"; /* 32 auf decimal */
-
- (void) printf("%d\n", strtol(strHexVal, (char **) NULL, 10));
-
- However, there is no convenient specifier to the printf family that
- converts to an arbitrary base (but you can do all the useful ones,
- namely 8, 10, and 16). If you want to do this, you'll have to
- code your own function to do so.
-
- Hope this helps,
- Greg
- --
- Greg Bacon <gbacon@cs.uah.edu>
- University of Alabama in Huntsville
- CS Department Systems Support Team
-